#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN DATA ABSTRACTION LAYER version 1

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
using System.Collections;
using Thn.Application.Sort;
#endregion

namespace Thn.Data.Query
{
    /// <summary>
    /// Represents the Update expression
    /// </summary>
    public class UpdateQuery : ConditionalQuery
    {
        #region Update Clause
        private object mUpdateClause;
        /// <summary>
        /// Gets data object(s) to Update to database
        /// <para>This can be a single object, an array of object, or a collection.</para>        
        /// </summary>
        public object UpdateClause
        { get { return mUpdateClause; } }
        #endregion

        #region Field Clause
        private ArrayList mFieldClause;
        /// <summary>
        /// Gets Fields information. 
        /// <para>If null, the fields will be inferred from object type.</para>
        /// <para>If set, the fields are Updated. The DAL will execute query against these fields only</para>
        /// </summary>
        public ArrayList FieldClause
        { get { return mFieldClause; } }
        #endregion               

        #region Fields
        /// <summary>
        /// Specifies the exact field names
        /// </summary>
        /// <param name="names">Names of Object's fields</param>
        public UpdateQuery Fields(params string[] names)
        {
            if (mFieldClause == null) mFieldClause = new ArrayList(names.Length);
            for (int i = 0; i < names.Length; i++)
            {
                mFieldClause.Add(names[i]);
            }

            return this;
        }
        #endregion                

        #region Constructors
        /// <summary>
        /// Create a new Update query for an array of objects
        /// </summary>
        public UpdateQuery(params object[] values)
        {
            mUpdateClause = values;
        }

        /// <summary>
        /// Create a new Update query for a collection of objects
        /// </summary>
        public UpdateQuery(ICollection values)
        {
            mUpdateClause = values;
        }
        #endregion

        #region To String
        /// <summary>
        /// Convert this query to text form.
        /// <para>NOTE: Do not use this method to obtain the SQL query. It will not be correct.</para>
        /// </summary>
        public override string ToString()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            //Update
            sb.Append("UPDATE ");            

            //FIELD
            if (mFieldClause != null)
            {
                for (int i = 0; i < mFieldClause.Count; i++)
                {
                    if (i > 0) sb.Append(", ");
                    sb.Append(mFieldClause[i].ToString());
                }
            }            

            //WHERE
            if (WhereClause != null)
            {
                sb.Append(" WHERE ");
                sb.Append(WhereClause.ToString());
            }

            return sb.ToString();
        }
        #endregion
    }
}
